package com.ruoyi.system.service.impl;

import java.util.Calendar;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.vo.ViteaNum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BywUserVitaeInfoMapper;
import com.ruoyi.system.domain.BywUserVitaeInfo;
import com.ruoyi.system.service.IBywUserVitaeInfoService;

/**
 * 用户资源包Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-24
 */
@Service
public class BywUserVitaeInfoServiceImpl implements IBywUserVitaeInfoService 
{
    @Autowired
    private BywUserVitaeInfoMapper bywUserVitaeInfoMapper;

    /**
     * 查询用户资源包
     * 
     * @param id 用户资源包主键
     * @return 用户资源包
     */
    @Override
    public BywUserVitaeInfo selectBywUserVitaeInfoById(Long id)
    {
        return bywUserVitaeInfoMapper.selectBywUserVitaeInfoById(id);
    }

    /**
     * 查询用户资源包列表
     * 
     * @param bywUserVitaeInfo 用户资源包
     * @return 用户资源包
     */
    @Override
    public List<BywUserVitaeInfo> selectBywUserVitaeInfoList(BywUserVitaeInfo bywUserVitaeInfo)
    {
        return bywUserVitaeInfoMapper.selectBywUserVitaeInfoList(bywUserVitaeInfo);
    }

    /**
     * 新增用户资源包
     * 
     * @param bywUserVitaeInfo 用户资源包
     * @return 结果
     */
    @Override
    public int insertBywUserVitaeInfo(BywUserVitaeInfo bywUserVitaeInfo)
    {
        return bywUserVitaeInfoMapper.insertBywUserVitaeInfo(bywUserVitaeInfo);
    }

    /**
     * 修改用户资源包
     * 
     * @param bywUserVitaeInfo 用户资源包
     * @return 结果
     */
    @Override
    public int updateBywUserVitaeInfo(BywUserVitaeInfo bywUserVitaeInfo)
    {
        bywUserVitaeInfo.setUpdateTime(DateUtils.getNowDate());
        return bywUserVitaeInfoMapper.updateBywUserVitaeInfo(bywUserVitaeInfo);
    }

    /**
     * 批量删除用户资源包
     * 
     * @param ids 需要删除的用户资源包主键
     * @return 结果
     */
    @Override
    public int deleteBywUserVitaeInfoByIds(Long[] ids)
    {
        return bywUserVitaeInfoMapper.deleteBywUserVitaeInfoByIds(ids);
    }

    /**
     * 删除用户资源包信息
     * 
     * @param id 用户资源包主键
     * @return 结果
     */
    @Override
    public int deleteBywUserVitaeInfoById(Long id)
    {
        return bywUserVitaeInfoMapper.deleteBywUserVitaeInfoById(id);
    }

    @Override
    public ViteaNum vitaeNum(Long sysUserId){
        BywUserVitaeInfo userVitaeInfo = new BywUserVitaeInfo();
        userVitaeInfo.setSysUserId(sysUserId);
        List<BywUserVitaeInfo> bywUserVitaeInfos = bywUserVitaeInfoMapper.selectBywUserVitaeInfoList(userVitaeInfo);
        if (bywUserVitaeInfos == null){
            return new ViteaNum(0,0);
        }
        int vitaeTotalNum = 0;
        int residueTotalNum = 0;
        for (BywUserVitaeInfo bywUserVitaeInfo : bywUserVitaeInfos){
            vitaeTotalNum += bywUserVitaeInfo.getVitaeNum();
            if (getDayAfterEndTime(System.currentTimeMillis(),0) <= getDayAfterEndTime(bywUserVitaeInfo.getExpiryTime().getTime(),0)){
                residueTotalNum += bywUserVitaeInfo.getResidueNum();
            }
        }
        return new ViteaNum(vitaeTotalNum, residueTotalNum);
    }

    @Override
    public int decreaseVitaeOne(Long sysUserId){
        BywUserVitaeInfo userVitaeInfo = new BywUserVitaeInfo();
        userVitaeInfo.setSysUserId(sysUserId);
        List<BywUserVitaeInfo> bywUserVitaeInfos = bywUserVitaeInfoMapper.selectBywUserVitaeInfoList(userVitaeInfo);
        if (bywUserVitaeInfos == null){
            return 0;
        }
        for (BywUserVitaeInfo bywUserVitaeInfo : bywUserVitaeInfos){
            Integer residueNum = bywUserVitaeInfo.getResidueNum();
            if (getDayAfterEndTime(System.currentTimeMillis(),0) <= getDayAfterEndTime(bywUserVitaeInfo.getExpiryTime().getTime(),0)){
                if (residueNum > 0){
                    bywUserVitaeInfo.setResidueNum(residueNum - 1);
                    bywUserVitaeInfo.setUpdateBy("system");
                    bywUserVitaeInfo.setUpdateTime(DateUtils.getNowDate());
                    bywUserVitaeInfoMapper.updateBywUserVitaeInfo(bywUserVitaeInfo);
                    return 1;
                }
            }
        }
        return 0;
    }




    public static long getDayAfterStartTime(long millis, int x) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millis);
        // 将日期设置为昨天
        calendar.add(Calendar.DATE, x);

        // 设置为昨天的开始时间
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    public static long getDayAfterEndTime(long millis, int x) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millis);
        // 将日期设置为昨天
        calendar.add(Calendar.DATE, x);

        // 设置为昨天的结束时间
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTimeInMillis();
    }
}
